import {
  getAcceptAllTime,
  getPrjDetailApi,
  updateAcceptTimeApi,
  getCommonBtnInfo,
  getShowQueryFsuEquipmentApi
} from '@/api/acceptance'
import { configButtonIsDisplayedApi } from '@/api/acceptanceManagement/acceptanceMange'

export const Mixins = {
  inject: ['prjDetail', 'workDetail'],
  props: {
    prjId: {
      type: String,
      required: true
    },
    hqType: {
      type: Boolean
    }
  },
  data() {
    return {
      isConfig: false,
      formData: {},
      rules: {},
      formList: [],
      selectList: [],
      onlyNengYuanBtns: [],
    }
  },
  computed: {
    newPrjDetail() {
      return this.prjDetail()
    },
    newWorkDetail() {
      return this.workDetail()
    },
    formOptions() {
      // 室分类项目
      // if (this.newPrjDetail.prjType === 'B' && this.formList.includes('endInstallDate')) {
      // if (!['F1', 'F1X', 'F7'].includes(this.newPrjDetail.accStatus)) {
      //   this.rules = {
      //     endYiDongInstallDate: [
      //       { required: true, message: '请选择设备安装完成时间', trigger: 'blur' }
      //     ],
      //     endLianTongInstallDate: [
      //       { required: true, message: '请选择设备安装完成时间', trigger: 'blur' }
      //     ],
      //     endDianXinInstallDate: [
      //       { required: true, message: '请选择设备安装完成时间', trigger: 'blur' }
      //     ]
      //   }
      // }
      // 移动
      // if (this.formData?.orderOperators.includes('1001')) {
      //   this.formList = [...this.formList, 'endYiDongInstallDate']
      // }
      // // 联通
      // if (this.formData?.orderOperators.includes('1002')) {
      //   this.formList = [...this.formList, 'endLianTongInstallDate']
      // }
      // // 电信
      // if (this.formData?.orderOperators.includes('1003')) {
      //   this.formList = [...this.formList, 'endDianXinInstallDate']
      // }
      // }
      // if (this.prjType !== 'B' && this.formList.includes('endInstallDate2')) {
      //   this.rules = {
      //     ...this.rules,
      //     endYiDongInstallDate2: [
      //       { required: true, message: ' ', trigger: 'blur' }
      //     ],
      //     endLianTongInstallDate2: [
      //       { required: true, message: ' ', trigger: 'blur' }
      //     ],
      //     endDianXinInstallDate2: [
      //       { required: true, message: ' ', trigger: 'blur' }
      //     ]
      //   }
      //   this.formList = [...this.formList, 'endYiDongInstallDate2', 'endLianTongInstallDate2', 'endDianXinInstallDate2']
      // }
      return this.formList
    },
    // 智联服务成本-购买服务
    noGouMaiSbLr() {
      return this.newPrjDetail.prjType === 'W' && this.newPrjDetail.buildModes === '25' && this.newPrjDetail.isNewBusiness === '1' && this.newPrjDetail.ifOnlineServ === '0'
    },
    btnjudgmentOne() {
      return +this.newPrjDetail.isNewRefactorCommPrj === 1 && this.newPrjDetail.businessMajor === '01' && +this.newPrjDetail.ifNewStartupProcess === 1&&this.newPrjDetail.manageMode!=='01'
    },
    btnjudgment() {
      return this.newPrjDetail.isNewBusiness === '2' || +this.newPrjDetail.isNewAcceptance === 1 || this.btnjudgmentOne
    },
    btnOptions() {
      console.log(this.newPrjDetail, 'this.newPrjDetail', this.newWorkDetail, this.btnjudgment, "=>>>")
      // 能源接口返回按钮
      if (this.btnjudgment) {
        return this.onlyNengYuanBtns
      }
      return this.btnList.filter(item => {
        if (['panorama', 'threeOrders'].includes(item)) { return false }
        if (this.handleShowBtnList(item)) { return false }
        if (this.newPrjDetail.prjYear < 2023 && ['maintain'].includes(item)) { return false }
        if (this.newPrjDetail.manageMode === '01' && !this.isConfig && ['serviceEdit', 'serviceShow'].includes(item)) { return false }
        return true
      })
    },
  },
  methods: {
    getCheckOpenEquipmentParams() {
      let params = [];
      if (this.newPrjDetail.manageMode === '01' || this.newPrjDetail.manageMode === '31') {
        //单站项目 单项工程 取自己
        params.push(this.newPrjDetail.prjId);
      } else {
        //整体项目 单项工程列表所有数据为子项目集合
        const _tableData = JSON.parse(window.sessionStorage.getItem('TableprjInfos')) || []
        for (let i = 0; i < _tableData?.length; i++) {
          params.push(_tableData[i]?.prjId)
        }
      }
      return params;
    },
    checkAddEquipmentChange() {
      //判断是综合查询、维护类项目、    未过资源确认节点(资源录入、资源确认)不显示（暂时不需要）
      if (this.$route.query.type === '1' && this.newPrjDetail.isNewBusiness === '0' && this.newPrjDetail.businessMajor === '04') {
        const params = this.getCheckOpenEquipmentParams();
        // const params=[
        //   "19731968"
        // ];
        console.log('params', params)
        getShowQueryFsuEquipmentApi(params).then((res) => {
          if (+res.code === 200 && res.data?.length > 0) {
            //调接口  判断是否显示设备变动按钮
            this.btnList?.push('equipmentChange')
            window.sessionStorage.setItem('equipmentPrjIds', JSON.stringify(res.data))
          }
        })
      }
    },
    async nengYuanButton() {
      if (this.newPrjDetail.isNewBusiness === '2' || +this.newPrjDetail.isNewAcceptance === 1 || this.btnjudgmentOne) {
        const params = {
          prjId: this.prjId,
          accStatus: this.newWorkDetail.bizObject.bizState,
          pageType: this.$route.query.type === '1' ? 'done' : this.$route.query.type,
          manageMode: this.newPrjDetail.manageMode
        }
        const { code, data } = await getCommonBtnInfo(params)
        if (+code === 200) {
          this.onlyNengYuanBtns = data?.map(item => item?.buttonCode)
        }
        console.log(this.onlyNengYuanBtns, '能源按钮接口返回')
      }
    },
    handleShowBtnList(item) {
      const _tablesinfo = JSON.parse(window.sessionStorage.getItem('TableprjInfos')) || []
      const singelZhengTi = _tablesinfo[0]?.prjType === 'W' && _tablesinfo[0]?.buildModes === '25' && this.newPrjDetail.ifOnlineServ === '0'
      const single = ['materialApplyRetreat', 'addValuation', 'serviceEdit', 'serviceShow']
      const zhengti = ['materialBinding', 'materialService', 'materialShow', 'serviceEdit', 'serviceShow', 'materialServiceShow']
      if (this.noGouMaiSbLr && this.newPrjDetail.manageMode === '01') {
        return single.includes(item)
      } else if (singelZhengTi) {
        return zhengti.includes(item)
      } else {
        return false
      }
    },
    commonButtonShow(btnList) {
      const newbackButton = ['projectStart', 'projectEnd']
      let allBtnList = btnList
      const _modesType = ['10', '09']
      const _mangeModeType = ['01', '03']
      // 单站 资源录入 资产购置||维护改造 无实施
      if (this.maintenancereconstruction(_modesType, _mangeModeType) && this.newPrjDetail.isNewBusiness === '0' && this.newPrjDetail.businessMajor === '04') {
        allBtnList = [...newbackButton, ...btnList]
        console.log(allBtnList, '维护改造类项目')
      }
      return allBtnList
    },
    maintenancereconstruction(_modesType, _mangeModeType) {
      return this.newWorkDetail.bizObject.bizState === 'F4' && _mangeModeType.includes(this.newPrjDetail.manageMode) && _modesType.includes(this.newPrjDetail.buildModes) && this.newPrjDetail.ifNewStartupProcess === '1'
    },
    handleClearList(list) {
      this.selectList = list
      console.log(this.selectList, '[]')
    },
    handleCheck(v) {
      this.selectList = v
      console.log(v)
    },
    getConfigButton() {
      const params = {
        ifSingleProject: '0',
        subPrjId: '',
        prjId: this.prjId
      }
      configButtonIsDisplayedApi(params).then(res => {
        this.isConfig = res.data
      })
    },
    submit() {
      let a = false
      this.$refs.enteringForm.submitForm(e => {
        a = e
      })
      if (a) {
        const data = {}
        // 移动
        if (this.formList.includes('endYiDongInstallDate') || this.formList.includes('endYiDongInstallDate2')) {
          data['endYiDongInstallDate'] = this.formData.endYiDongInstallDate
        }
        // 联通
        if (this.formList.includes('endLianTongInstallDate') || this.formList.includes('endLianTongInstallDate2')) {
          data['endLianTongInstallDate'] = this.formData.endLianTongInstallDate
        }
        // 电信
        if (this.formList.includes('endDianXinInstallDate') || this.formList.includes('endDianXinInstallDate2')) {
          data['endDianXinInstallDate'] = this.formData.endDianXinInstallDate
        }
        // 验收
        if (this.formList.includes('endInternalAcceptDate') && this.getEndInternalAcceptDate()) {
          data['endInternalAcceptDate'] = this.formData.endInternalAcceptDate
        }
        return { code: 200, data }
      } else {
        return { code: 500 }
      }
    },
    //校验是否验收信息
    getEndInternalAcceptDate(){
        if(this.newPrjDetail.manageMode === '01' && this.newPrjDetail.prjType === 'Y'){
          return true;
        }
        if(this.getSoftDevService()){
          return true;
        }
        return false;
    },
    //是否满足软件开发类和服务成本
    getSoftDevService(){
        if(['51', '05'].includes(this.newPrjDetail.manageMode)){
          return true;
        }
        if(this.newPrjDetail.manageMode === '01' && this.newPrjDetail.prjType === 'W' && this.newPrjDetail.buildModes === '25' && this.newPrjDetail.isSingleAccounting === '1'){
          return true;
        }
        return false;
    },
    getNowDay(){
        let time = new Date();
        let year=time.getFullYear();
        let month=time.getMonth()+1;
        let day=time.getDate();
        return year+'-'+(month<10?'0'+month:month)+'-'+(day<10?'0'+day:day)
    },
    async getTime() {
      const result = await getAcceptAllTime(this.prjId)
      console.log(result)
      if (!result.data) {
        result.data = {}
      }
      if (!result.data.orderOperators) {
        result.data.orderOperators = []
      }
      let nowDay = this.getNowDay();
      let saveTime = result.data.endInternalAcceptDate==null? nowDay : result.data.endInternalAcceptDate
      let rentableOperateTime = result.data.rentableOperateTime==null? '' : result.data.rentableOperateTime
      window.sessionStorage.setItem('qizuTime', rentableOperateTime)
      if(this.newPrjDetail.manageMode === '51'){
        window.sessionStorage.setItem('acceptTimeDate', saveTime)
      }
      this.formData = {
        ...result.data,
        endInternalAcceptDate:this.getSoftDevService() &&  this.$route.query.type === 'todo'? saveTime : result.data.endInternalAcceptDate
      }
    }
  },
  created() {
    this.checkAddEquipmentChange(); //判断综合查询是否添加设备变动信息
    this.nengYuanButton()
    if (this.newPrjDetail.manageMode === '01') {
      this.getConfigButton()
    } else {
      this.isConfig = true
    }

    this.getTime()
  },
  submit() {
    let a = false
    this.$refs.enteringForm.submitForm(e => {
      a = e
    })
    updateAcceptTimeApi({
      prjId: this.prjId
    })
    if (a) {
      return { code: 200 }
    } else {
      return { code: 500 }
    }
  }
}
